package com.example.administrator.maizuofilm.hot;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.maizuofilm.BaseFragment;
import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.bean.Hot;
import com.example.administrator.maizuofilm.hot.adapter.HotAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/7/12.
 */
public class HotFragment extends BaseFragment implements HotContract.HotView {
    @InjectView(R.id.recycler_hot_movie)
    RecyclerView recyclerHotMovie;
    private HotModel model;
    private HotPresenter presenter;
    private HotAdapter adapter;
    private List<Hot.DataBean.FilmListBean> hots;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hot, null);
        ButterKnife.inject(this, view);
        hots = new ArrayList<>();
        adapter = new HotAdapter(hots,getActivity());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerHotMovie.setLayoutManager(linearLayoutManager);
        recyclerHotMovie.setAdapter(adapter);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        model = new HotModel();
        presenter = new HotPresenter(this, model);
        presenter.init();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onInitSuccess(Hot hot) {
        hots.addAll(hot.getData().getFilmList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onInitFail(String failMsg) {
        Log.i("tag", failMsg);
    }
}
